import React from "react";
import BaseComponent from "../../redare/components/BaseComponent";
import {Input, Modal} from "antd";
import RUserSelectGrid from "./RUserSelectGrid";


export default class RUserSelect extends BaseComponent {


    static defaultProps = {
        labelField: 'name',
        valueField: 'id',
    };


    constructor(props) {
        super(props);
        this.state = {
            value: ''
        }
    }

    handleChange = (e) => {
        this.setState({value: e.target.value});
    }

    setText = (text) => {
        this.setState({value: text})
    }

    render() {
        let {onChange, labelField, valueField} = this.props;
        return (
            <>
                <Input
                    allowClear={true}
                    value={this.state.value}
                    placeholder={'请选择用户'}
                    onChange={(e)=>{
                        if (!e.target.value||e.target.value===''){
                            if (onChange) {
                                onChange(e.target.value)
                            }
                        }
                        this.setState({
                            value: e.target.value
                        })
                    }}
                    editable={false} onClick={() => {
                    this.setState({
                        visible: true
                    })
                }
                }/>
                <Modal
                    closable={true}
                    title={'用户选择'}
                    visible={this.state.visible}
                    width={900}
                    onOk={() => {
                        if (this.grid) {
                            let rows = this.grid.getSelectData()
                            if (!rows || rows.length < 1) {
                                this.toastFail('请选择数据');
                                return;
                            }
                            let arr1 = []
                            let arr2 = []
                            for (let item of rows) {
                                arr1.push(item[valueField])
                                arr2.push(item[labelField])
                            }
                            let valueStr = arr1.join(',')
                            let textStr = arr2.join(',')
                            if (onChange) {
                                onChange(valueStr,textStr,rows,arr1, arr2)
                            }
                            this.setState({
                                visible: false,
                                value: textStr
                            })
                        }
                    }}
                    onCancel={() => {
                        this.setState({
                            visible: false
                        })
                    }}
                >
                    <RUserSelectGrid ref={(o) => {
                        this.grid = o
                    }}/>
                </Modal>
            </>

        )
    }

}





